package br.com.unitunes.dao;

import java.util.List;

import javax.persistence.Query;
import br.com.caelum.vraptor.ioc.Component;
import br.com.unitunes.model.Video;

@Component
public class VideoDAO extends GenericDAO<Video>{
	
	@Override
	public Class<Video> getModelClass() {
		return Video.class;
	}
	
	public List<Video> pesquisa(String termo, Long index, Long maxResults, Long autorID) {
		
		String queryStr = "SELECT video FROM VIDEO video WHERE ( UPPER(video.nome) LIKE UPPER(:nome) OR UPPER(video.descricao) LIKE UPPER(:descricao) )"; 
		if(autorID != null) {
			queryStr += " AND video.autor.id = :autorID";
		}
		
		Query query = getEntityManager().createQuery(queryStr);
		
		query.setParameter("nome", '%' + (termo != null ? termo : "") + '%');
		query.setParameter("descricao", '%' + (termo != null ? termo : "") + '%');

		if(autorID != null) {
			query.setParameter("autorID", autorID);
		}		
		
		return executaQuery(query, maxResults, index);
	}
}
